﻿Imports System.Windows.Forms
Imports System.Drawing.Color
Imports Microforma.BLL
Public Class frmIndizarDocumentos
    Inherits System.Windows.Forms.Form


    'Controles
    Private txtTexto(0) As TextBox
    Private TxtNumeroConDecimal(0) As TextBox
    Private TxtNumeroSinDecimal(0) As TextBox
    Private DtpFecha(0) As DateTimePicker
    Private CboVerdaderoFalso(0) As ComboBox
    Private CboListaSeleccion(0) As ComboBox
    Private CboListaMultiple(0) As ListView

    Private lblTituloIndice(0) As Label

    'Distancia entre controles
    Dim IntDistanciaEntreTitulos As Byte = 40
    Dim IntDistanciaTituloControl As Byte = 25
    Dim IntPosicionLabelTitulo As Integer
    Public PubIdDocumento As String
    Dim beDocumento As BLL.DocumentosBL
    Dim StrNombreInterno As String
    Sub AgregaTexto()

        txtTexto(txtTexto.Length - 1) = New TextBox
        With Me.txtTexto(txtTexto.Length - 1)
            .Text = "Ingrese los datos del textbox"
            .Location = New System.Drawing.Point(20, IntPosicionLabelTitulo - IntDistanciaTituloControl)
            .Width = 300
            .MaxLength = 200
            .Tag = StrNombreInterno
        End With
        Me.Controls.Add(txtTexto(txtTexto.Length - 1))
        ReDim txtTexto(txtTexto.Length)

    End Sub
    Sub AgregarTxtNumeroConDecimal()
        'Agrega label
               
        TxtNumeroConDecimal(TxtNumeroConDecimal.Length - 1) = New TextBox

        With Me.TxtNumeroConDecimal(TxtNumeroConDecimal.Length - 1)
            .Text = "00"
            .Location = New System.Drawing.Point(20, IntPosicionLabelTitulo - IntDistanciaTituloControl)
            .Width = 100
            .MaxLength = 10
            .Tag = StrNombreInterno
        End With

        Me.Controls.Add(TxtNumeroConDecimal(TxtNumeroConDecimal.Length - 1))
        ReDim TxtNumeroConDecimal(TxtNumeroConDecimal.Length)

    End Sub
    Sub AgregarTxtNumeroSinDecimal()
        'Agrega label

        TxtNumeroSinDecimal(TxtNumeroSinDecimal.Length - 1) = New TextBox

        With Me.TxtNumeroSinDecimal(TxtNumeroSinDecimal.Length - 1)
            .Text = "00.00"
            .Location = New System.Drawing.Point(20, IntPosicionLabelTitulo - IntDistanciaTituloControl)
            .Width = 100
            .MaxLength = 10
            .Tag = StrNombreInterno
        End With

        Me.Controls.Add(TxtNumeroSinDecimal(TxtNumeroSinDecimal.Length - 1))
        ReDim TxtNumeroSinDecimal(TxtNumeroSinDecimal.Length)

    End Sub
    Sub AgregarFecha()
        'Agrega label

        DtpFecha(DtpFecha.Length - 1) = New DateTimePicker
        With Me.DtpFecha(DtpFecha.Length - 1)
            .Location = New System.Drawing.Point(20, IntPosicionLabelTitulo - IntDistanciaTituloControl)
            .Width = 300
            .Tag = StrNombreInterno
        End With

        Me.Controls.Add(DtpFecha(DtpFecha.Length - 1))
        ReDim DtpFecha(DtpFecha.Length)

    End Sub
    Sub AgregarVerdaderoFalso()
        'Agrega label

        CboVerdaderoFalso(CboVerdaderoFalso.Length - 1) = New ComboBox
        With Me.CboVerdaderoFalso(CboVerdaderoFalso.Length - 1)
            .Items.Add("Verdadero")
            .Items.Add("Falso")
            .Location = New System.Drawing.Point(20, IntPosicionLabelTitulo - IntDistanciaTituloControl)
            .Width = 100
            .SelectedIndex = 0
            .Tag = StrNombreInterno
        End With

        Me.Controls.Add(CboVerdaderoFalso(CboVerdaderoFalso.Length - 1))
        ReDim DtpFecha(CboVerdaderoFalso.Length)

    End Sub
    Sub AgregarListaSeleccion(ByVal ArrayItems() As String)
        'Agrega label
        Dim Intcontador As Integer
       
        CboListaSeleccion(CboListaSeleccion.Length - 1) = New ComboBox
        With Me.CboListaSeleccion(CboListaSeleccion.Length - 1)

            For Intcontador = 0 To ArrayItems.Length - 1
                .Items.Add(ArrayItems(Intcontador))
            Next

            .Location = New System.Drawing.Point(20, IntPosicionLabelTitulo - IntDistanciaTituloControl)
            .Width = 100
            .SelectedIndex = 0
            .Tag = StrNombreInterno
        End With

        Me.Controls.Add(CboListaSeleccion(CboListaSeleccion.Length - 1))
        ReDim CboListaSeleccion(CboListaSeleccion.Length)

    End Sub

    Sub AgregarListaSeleccionMultiple(ByVal ArrayItems() As String)
        'Agrega label
        Dim Intcontador As Integer
        Dim lviItem As ListViewItem

        CboListaMultiple(CboListaMultiple.Length - 1) = New ListView
        With Me.CboListaMultiple(CboListaMultiple.Length - 1)
            .View = View.Details
            .Columns.Add("Seleccione los elementos", 200, HorizontalAlignment.Right)
            For Intcontador = 0 To ArrayItems.Length - 1
                lviItem = New ListViewItem
                lviItem.Text = ArrayItems(Intcontador)
                .Items.Add(lviItem)
            Next
            .Height = 100
            .Location = New System.Drawing.Point(20, IntPosicionLabelTitulo - IntDistanciaTituloControl)
            .Width = 300
            .CheckBoxes = True
            .Tag = StrNombreInterno
        End With

        Me.Controls.Add(CboListaMultiple(CboListaMultiple.Length - 1))
        ReDim CboListaMultiple(CboListaMultiple.Length)

    End Sub

    Sub AgregaTituloIndice(ByVal TituloIndice As String)

        lblTituloIndice(lblTituloIndice.Length - 1) = New Label
        With Me.lblTituloIndice(lblTituloIndice.Length - 1)
            .Text = TituloIndice
            .Location = New System.Drawing.Point(LblTituloTipoDocumental.Left, IntPosicionLabelTitulo)
            .Size() = New System.Drawing.Size(65, 15)
            .Width = 200
        End With
        Me.Controls.Add(lblTituloIndice(lblTituloIndice.Length - 1))
        ReDim lblTituloIndice(lblTituloIndice.Length)
        IntPosicionLabelTitulo = IntPosicionLabelTitulo + IntDistanciaEntreTitulos


    End Sub
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Dim StrSQL, StrCampos, StrValores As String
        Dim StrTabla As String = CboTiposDocumentales.Text.Substring(CboTiposDocumentales.Text.IndexOf("-") + 1)

        If CboTiposDocumentales.Text.Trim = "" Then
            MsgBox("Seleccione un tipo documental para el documento.", MsgBoxStyle.Information, Me.Text)
            Exit Sub
        End If

        StrSQL = "Insert into " & StrTabla.Replace(" ", "")


        Dim Control As Windows.Forms.Control
        For Each Control In Me.Controls

            If Control.GetType Is GetType(System.Windows.Forms.TextBox) Then
                StrCampos = StrCampos & Control.Tag & ","
                StrValores = StrValores & "'" & Control.Text & "',"
            ElseIf Control.GetType Is GetType(System.Windows.Forms.DateTimePicker) Then
                StrCampos = StrCampos & Control.Tag & ","
                StrValores = StrValores & "'" & CType(Control, DateTimePicker).Value.ToString("dd/MM/yy") & "',"
            ElseIf Control.GetType Is GetType(System.Windows.Forms.ComboBox) And Control.Name <> "CboTiposDocumentales" Then
                StrCampos = StrCampos & Control.Tag & ","
                Select Case CType(Control, ComboBox).Text
                    Case "Falso"
                        StrValores = StrValores & "'0',"
                    Case "Verdadero"
                        StrValores = StrValores & "'1',"
                    Case Else
                        StrValores = StrValores & "'" & CType(Control, ComboBox).Text & "',"
                End Select

            ElseIf Control.GetType Is GetType(System.Windows.Forms.ListView) Then
                StrCampos = StrCampos & Control.Tag & ","
                StrValores = StrValores & "'" & CType(Control, ListView).SelectedItems(0).Text & "',"
            End If
        Next

        StrCampos = StrCampos.Substring(0, StrCampos.Length - 1)
        StrValores = StrValores.Substring(0, StrValores.Length - 1)

        StrSQL = StrSQL & "(IdDocumento," & StrCampos & ") values (" _
                        & PubIdDocumento & "," & StrValores & ")"

        beDocumento.AgregarMetada(StrSQL)

    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub frmIndizarDocumentos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        IntPosicionLabelTitulo = LblTituloTipoDocumental.Top + IntDistanciaEntreTitulos
        Call CargarTiposDocumentales()


        'Call AgregarTxtNumeroConDecimal()
        'Call AgregarTxtNumeroSinDecimal()
        'Call AgregarFecha()
        'Call AgregarVerdaderoFalso()

        'Dim arrays() As String = {"Uno", "Dos", "Tres", "Cuatro"}

        'Call AgregarListaSeleccion(arrays)

        'Call AgregarListaSeleccionMultiple(arrays)

        beDocumento = New BLL.DocumentosBL
    End Sub

    Sub CargarTiposDocumentales()
        Dim tiposDocBl As New TypeDocBL
        Dim _DataTableTiposDoc As DataTable = tiposDocBl.GetListAll
        Dim StrTextoAdd As String

        For Each dtrFila As DataRow In _DataTableTiposDoc.Rows
            StrTextoAdd = dtrFila("IdPlantilla") & "-" & dtrFila("Nombre")
            CboTiposDocumentales.Items.Add(StrTextoAdd)
        Next


    End Sub

    Private Sub CboTiposDocumentales_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboTiposDocumentales.SelectedIndexChanged

        IntPosicionLabelTitulo = LblTituloTipoDocumental.Top + IntDistanciaEntreTitulos
        'Elimina controles
        For intContador As Integer = Me.Controls.Count - 1 To 3 Step -1
            Me.Controls.RemoveAt(intContador)
        Next

        Dim indicesBL As New IndicesBL
        Dim StrIdTipoDocumental As String = CboTiposDocumentales.Text
        StrIdTipoDocumental = StrIdTipoDocumental.Substring(0, StrIdTipoDocumental.IndexOf("-"))
        Dim IntTipoDato, IdLista As Byte
        Dim StrNombreIndice As String

        Dim _DataTableIndices As DataTable = indicesBL.List(CInt(StrIdTipoDocumental))

        For Each dtrFila As DataRow In _DataTableIndices.Rows
            IntTipoDato = dtrFila("IdTipoDato")
            StrNombreIndice = dtrFila("Nombre")
            StrNombreInterno = dtrFila("NombreInterno")
            IdLista = IIf(dtrFila("IdLista") & "" = "", 0, dtrFila("IdLista"))
            Call AgregaIndiceSegunTipo(IntTipoDato, StrNombreIndice, IdLista)
        Next
    End Sub
    Private Sub AgregaIndiceSegunTipo(ByVal IntTipo As Integer, ByVal Titulo As String, _
                                    ByVal IdLista As Byte)
        Call AgregaTituloIndice(Titulo)

        Select Case IntTipo
            Case 1
                Call AgregaTexto()
            Case 2
                Call AgregarTxtNumeroSinDecimal()
            Case 3
                Call AgregarTxtNumeroConDecimal()
            Case 4
                Call AgregarFecha()
            Case 5
                Call AgregarVerdaderoFalso()
            Case 6
                Call AgregarListaSeleccion(ObtieneArrayListas(IdLista))
            Case 7
                Call AgregarListaSeleccionMultiple(ObtieneArrayListas(IdLista))
        End Select
    End Sub
    Function ObtieneArrayListas(ByVal IdLista As Integer) As String()
        Dim listaBL As New ListasBL

        Dim _DataTableListas As DataTable = listaBL.List(IdLista)
        Dim arreglo As String()

        arreglo = Split(_DataTableListas.Rows(0)("Contenido"), " ")

        Return arreglo

    End Function
End Class
